package com.cqhilink.iot.jobstandardmanagement.services.outerservice.impl;

import com.cqhilink.iot.jobstandardmanagement.facade.model.*;
import com.cqhilink.iot.jobstandardmanagement.facade.service.OutEmployeeService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.employee.JsmEmployeeService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.organization.JsmOrganizationService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.permission.JsmAccountRoleService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.permission.JsmRolesService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.post.JsmPostService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zmling on 2016/12/14.
 */

/**
 * 人员信息
 */
public class OutEmployeeServiceImpl implements OutEmployeeService {
    @Autowired
    private JsmEmployeeService jsmEmployeeService ;

    @Autowired
    private JsmPostService postService;
    @Autowired
    private JsmOrganizationService organizationService;
    @Autowired
    private JsmRolesService roleService;
    @Autowired
    private JsmAccountRoleService accountRoleService;
    /**
     * 查询所有人员信息
     *
     * @param employee
     * @return
     * @throws Exception
     */
    @Override
    public List<EmployeeVo> findEmployeeList(Employee employee) throws Exception {
        return jsmEmployeeService.findEmployees(employee);
    }

    public List<EmployeeVo> findEmployeeByLoginId(Employee employee) throws Exception {
        List<EmployeeVo> list = new ArrayList<>();
        List<EmployeeVo> voList = jsmEmployeeService.queryList(employee) ;
        if(voList != null && voList.size() > 0)
        for(EmployeeVo employeeVo: voList){
            // 判断当前类型
            if(employeeVo.getPostId()==null){
                employeeVo.setPostName("--");
            }else{
                postVo postVo = postService.toPostView(employeeVo.getPostId());
                // 操作岗位
                if(postVo != null && postVo.getPostName()!=null){
                    employeeVo.setPostName(postVo.getPostTypeName());
                }

            }
            // 根据组织机构获取作业区
            if(employeeVo.getOrgId()!=null){
                Organization organization = organizationService.selectOrgByOrgId(employeeVo.getOrgId());
                    /**
                     * 1,2,3
                     */
                   if(Integer.parseInt(organization.getOrgLevelId()) <=3){
                       employeeVo.setShowOrgName(organization.getOrgName());
                       employeeVo.setShowOrgId(organization.getOrgId());
                   } else{
                     List<Organization> organizationList =  organizationService.getOrganizationList(employeeVo);
                       if(organizationList!=null && organizationList.size() >0){
                        for(Organization org:organizationList){
                            if(org.getOrgLevelId().equals("3")){
                                employeeVo.setShowOrgName(org.getOrgName());
                                employeeVo.setShowOrgId(org.getOrgId());
                            }
                        }
                       }
                   }
            }
            // 查询当前系统角色
            String roleName = "";

            List<AccountRole> accountRoles = accountRoleService.getRolesByAccount(employeeVo);
            if (accountRoles != null && accountRoles.size() > 0) {
                int splitInt = 0;
                for (AccountRole accountRole : accountRoles) {
                    Roles roles = roleService.rolesSelect(accountRole.getRoleId());
                    //
                    if (roles != null && roles.getRoleName() != null) {
                        if (splitInt == 0) {
                            roleName = roles.getRoleName();
                        } else {
                            roleName = roleName + "," + roles.getRoleName();
                        }
                    }
                    splitInt++;
                }
            }
            employeeVo.setRolesName(roleName);
            list.add(employeeVo);
        }
        return list;
    }

    /**
     * 加载人员混合树
     * @param orgId
     * @param sync
     * @param openOrg
     * @param openDept
     * @param keyword
     * @return
     * @throws Exception
     */
    @Override
    public List<Ztree> loadEmployeeZtree(String orgId, boolean sync, boolean openOrg, boolean openDept, String keyword) throws Exception {
        return jsmEmployeeService.loadEmployeeZtree(orgId, sync, openOrg, openDept, keyword);
    }

    /**
     *
     * @param orgId               = null 查询全部组织机构
     * @param isLoadOrg           = true  加载组织树
     * @param isLoadDept          = true 加载组织部门树
     * @param isLoadOrgEmployee   = true 加载组织机构人员
     * @param isLoadDeptEmployee = true 加载组织部门人员
     * @param keyword    搜索值
     * @param maxLevel   获取最大层级{气矿分公司，等其他问题}
     * @param lessLevel  获取最小层级
     */
    @Override
    public List<Ztree> loadEmployeeZtree(String orgId, boolean isLoadOrg, boolean isLoadDept, boolean isLoadOrgEmployee, boolean isLoadDeptEmployee,String maxLevel,String lessLevel, String keyword) throws Exception {
        return jsmEmployeeService.loadEmployeeZtree(orgId,isLoadOrg, isLoadDept, isLoadOrgEmployee, isLoadDeptEmployee, maxLevel ,lessLevel,keyword);
    }


    /**
     * 根据ID查询信息
     *
     * @param employeeId
     * @return Employee
     * @Author 卢曼成
     */
    @Override
    public Employee findById(String employeeId) {
        return jsmEmployeeService.queryByKeyId(employeeId);
    }

    /**
     * 根据ID集合查询数据
     *
     * @param list
     * @return
     */
    @Override
    public List<Employee> selectByKeyList(List<String> list) {
        return jsmEmployeeService.selectByKeyList(list);
    }

    /**
     * 查询作业区下所有角色名称为XXX的人员
     * Author:cp
     */
    public List<Employee> queryEmployeeByAreaOrgIdAndRoleName(String areaOrgId,String roleName){
        return jsmEmployeeService.queryEmployeeByAreaOrgIdAndRoleName(areaOrgId,roleName);
    }
}
